//任务调度器
const EventEmitter = require('events');
const loadHtml = require('./node-downloader').loadHtml;
const Spider = require('./node-spider').Spider;
const Request = require('./node-request').Request;
const Response = require('./node-response').Response;


// const spider = new Spider();
const event = new EventEmitter();
var header = {Accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
    "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36"};
var cookie = '';
/**
 * 事件event ::::default-parse默认调用的解析方法 ::::custom-parse
 *
 */

module.exports.NodeScheduler=class {
    constructor(){
        this.pipeline = null;
        this.spider=null;
        this.downloader = null;
        this.cookie = '';
    }
    run(spider){
        this.spider = spider;
        console.log("程序开始...");
        let urls = spider.urls;
        console.log('程序开始运行...起始url:'+urls);
        //下载完网页

        event.on('custom-parse',(resp,callback)=>{

            if(callback==null){
                console.log("初始化调用");
                callback = spider.parse.bind(this.spider);
            }
            //绑定作用this
            callback = callback.bind(this.spider);
            for (let result of callback(resp)) {
                if (result.__proto__ == Request.prototype) {
                    cookie = resp.headers['set-cookie'];
                    this.addToCrawl(result);
                } else {
                    this.handle_data(result);
                }
            }

        });
        for (let url of urls){
            this.addToCrawl(new Request({url:url}));
        }
    }

    //添加到下载队列
    addToCrawl(request){
        if (request.cookie){
            request.headers['Cookie']=cookie;
        }
        // loadHtml(request,(text)=>event.emit('custom-parse',text,request.callback));
        this.downloader.addRxRequest(request,(text)=>event.emit('custom-parse',text,request.callback));
    }
    //处理数据
    handle_data(object){
        // console.log('抽取对象'+object.toString());
        this.pipeline.handle(object,this.spider);
    }
    setPipeline(pipeline){
        this.pipeline = pipeline;
    }

    setDownloader(downloader){
        this.downloader = downloader;
    }
}
